runOnStartup(async runtime => {
    window.addEventListener("minerTick", (e) => {
        hook_HookGetMiner.hookTouchMiner()
    })
    window.addEventListener("minerInit", (e) => {
        hook_HookGetMiner.hookgetMinerandBack()
        hook_HookGetMiner.hookGetMinerAndGetMoney()
        hook_HookGetMiner.hookgetMNinerScoreMix()
   })
});
globalThis.hook_HookGetMiner = {
    hookTouchMiner: function () {
        if (runtime.objects.ToolHook.getFirstInstance() == null || runtime.objects.ToolHook.getFirstInstance().isVisible==false) return
        let minerGameplayer = runtime.objects.point_minerGamePlaye.getFirstInstance()
        if (minerGameplayer == null) return
        if(Hook_shootHook.ismove==false) return; // 检测是否已经被发射出去
        for (let minerf of runtime.objects.f_miner.instances()) {
            var hook = runtime.objects.ToolHook.getFirstInstance()
            if (minerf.testOverlap(hook)) {
                if (hook.instVars.isGetMiner) return;
                minerf.x = hook.getImagePointX(1)
                minerf.y = hook.getImagePointY(1)
                hook.addChild(minerf,
                    {
                        transformX: true,
                        transformY: true,
                        transformWidth: false,
                        transformHeight: false,
                        transformAngle: true,
                        transformZElevation: true,
                        destroyWithParent: true
                    })
                hook.instVars.isGetMiner = true
                var effgetMiner = runtime.objects.P_getMiner.createInstance("Miner",minerf.x,minerf.y,true)
                Hook_shootHook.shootBackSoon()
           }
        }
   },
   hookgetMinerandBack: function () {
        window.addEventListener("hookBack", () => {
            var hook = runtime.objects.ToolHook.getFirstInstance();
            if (hook == null) return
            let miner = hook.getChildAt(0);
            if (miner == null) return;
            miner.destroy();
            hook.instVars.isGetMiner = false
        })
   },
   hookGetMinerAndGetMoney: function () {
        window.addEventListener("hookBack", () => {
            var hook = runtime.objects.ToolHook.getFirstInstance();
            if (hook == null) return
            /**
             * @type {ISpriteInstance}
             */
            var miner = hook.getChildAt(0);
            if (miner == null) return;
            runtime.globalVars.Money += miner.instVars.Money
        })
    },
   hookgetMNinerScoreMix: function () {
        window.addEventListener("hookBack",()=>{
            var hook =runtime.objects.ToolHook?.getFirstInstance()
            var getMiner =hook?.getChildAt(0)
            var MinerType =getMiner?.instVars?.type
            switch (MinerType){
                case "rope" :
                    //console.log("get rope")
                    runtime.globalVars.RopeDistance+=30
                    runtime.globalVars.RopeDurability+=50
                    break
           }
       })
   }
}
export default hook_HookGetMiner